{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "36058a07-507d-40dd-ba12-b6bed73a8817",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "import torch\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fcb29349-85af-46cf-9d4f-4abba34a0965",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"The cat had no business entering the neighbors garage, but\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82ea656a-947a-4567-800b-4e8ec94cf429",
   "metadata": {},
   "outputs": [],
   "source": [
    "generator = pipeline('text-generation', model='gpt2', device=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec2738bd-d25a-4740-997e-0775dc88aafd",
   "metadata": {},
   "outputs": [],
   "source": [
    "generated_sentences = generator(text, do_sample=True, max_length=30, num_return_sequences=5, num_beams=5, pad_token_id=50256)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fa42038d-cb7d-4b40-ab03-c88b401b0b23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cat had no business entering the neighbors garage, but when he arrived, he found the house empty.\n",
      "\n",
      "\"It was like he was in\n",
      "The cat had no business entering the neighbors garage, but when he arrived, he found the house empty.\n",
      "\n",
      "\"It was like he was going\n",
      "The cat had no business entering the neighbors garage, but when he arrived, he found the house empty.\n",
      "\n",
      "\"It was like he was just\n",
      "The cat had no business entering the neighbors garage, but when he arrived, he found the house empty.\n",
      "\n",
      "\"It was like he was a\n",
      "The cat had no business entering the neighbors garage, but when he arrived, he found the house empty.\n",
      "\n",
      "\"It was like he had no\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[None, None, None, None, None]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[print(generated_sentence['generated_text']) for generated_sentence in generated_sentences]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "852e3112-ad84-474e-9885-72a599ae19df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cat had no business entering the neighbors garage, but he did manage to get in.\n",
      "\n",
      "\"I'm not sure if it's because he\n",
      "The cat had no business entering the neighbors garage, but he did manage to get in.\n",
      "\n",
      "\"I'm not sure if it's because I\n",
      "The cat had no business entering the neighbors garage, but he did manage to get in.\n",
      "\n",
      "\"I'm not sure if it's a good\n",
      "The cat had no business entering the neighbors garage, but he did manage to get in.\n",
      "\n",
      "\"I'm not sure if it's because she\n",
      "The cat had no business entering the neighbors garage, but he did manage to get in.\n",
      "\n",
      "\"I'm not sure if it was my cat\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[None, None, None, None, None]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generated_sentences = generator(text, do_sample=True, max_length=30, num_return_sequences=5, num_beams=5, no_repeat_ngram_size=2,  pad_token_id=50256)\n",
    "[print(generated_sentence['generated_text']) for generated_sentence in generated_sentences]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c197505c-9d68-49cb-b1ad-99e36a2e8e42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cat had no business entering the neighbors garage, but when she got out of the car, she ran into the neighbor's house.\n",
      "\n",
      "\"\n",
      "The cat had no business entering the neighbors garage, but when she got out of the car, she ran into the neighbor's house.\n",
      "\n",
      "The\n",
      "The cat had no business entering the neighbors garage, but when she got out of the car, she ran into the neighbor's garage door.\n",
      "\n",
      "\n",
      "The cat had no business entering the neighbors garage, but when she got out of the car, she ran into the neighbor's house.\n",
      "\n",
      "She\n",
      "The cat had no business entering the neighbors garage, but when she got out of the car, she ran into the neighbor's house.\n",
      "\n",
      "When\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[None, None, None, None, None]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generated_sentences = generator(text, do_sample=True, max_length=30, num_return_sequences=5, num_beams=5, no_repeat_ngram_size=2,\n",
    "                               top_k=50, pad_token_id=50256)\n",
    "[print(generated_sentence['generated_text']) for generated_sentence in generated_sentences]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "352e9ac0-f84c-4f30-9885-ab8b543187ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cat had no business entering the neighbors garage, but it did get inside.\n",
      "\n",
      "\"That's when I realized it was time to get out of there,\" she said. \"I didn't know if I was going to be able to do anything about it. I just wanted to go home.\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[None]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generated_sentences = generator(text, do_sample=True, max_length=500, num_return_sequences=1, num_beams=5, no_repeat_ngram_size=2,\n",
    "                               top_k=50, top_p=0.85, pad_token_id=50256)\n",
    "[print(generated_sentence['generated_text']) for generated_sentence in generated_sentences]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
